﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tank_Control
{
    public partial class StepForm : Form
    {
        TankDevice Device; //Класс реализующий работу с девайсом

        Boolean but_status = true;

        public StepForm()
        {
            InitializeComponent();

            try
            {
                Device = new TankDevice(9600, 100, 100);
                Device.LogMsg += new WriteMsgToLog(ConsoleAppendThreadSafe);
               
                ConsoleAppend("Девайс успешно подключен (" + Device.PortName + ")", ConsoleMessageType.Info);
            }
            catch (Exception ex)
            {
                ConsoleAppend(ex.Message, ConsoleMessageType.Error);
            }
        }

        private void ConsoleAppendThreadSafe(string msg, ConsoleMessageType msg_type)
        {
            this.Invoke(new WriteMsgToLog(ConsoleAppend), msg, msg_type);
        }

        private void ConsoleAppend(string msg, ConsoleMessageType msg_type)
        {
            Color color = Color.Black;
            string tag = "";

            switch (msg_type)
            {
                case ConsoleMessageType.Info:
                    color = Color.Green;
                    tag = "INFO: ";
                    break;
                case ConsoleMessageType.Warning:
                    color = Color.Blue;
                    tag = "WARNING: ";
                    break;
                case ConsoleMessageType.Error:
                    color = Color.Red;
                    tag = "ERROR: ";
                    break;
            }
            string line = tag + msg + Environment.NewLine;
            int old_console_len = ConsoleBox.Text.Length;
            ConsoleBox.Select(Int32.MaxValue, 0);
            ConsoleBox.AppendText(line);
            ConsoleBox.Select(old_console_len, ConsoleBox.Text.Length);
            ConsoleBox.SelectionColor = color;
            ConsoleBox.Select(Int32.MaxValue, 0);
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            double temp;

            temp = Convert.ToDouble(textBox2.Text);

            temp = temp * (1.8);

            textBox1.Text = temp.ToString();
        }

        private void CWStep_Click(object sender, EventArgs e)
        {
            Device.ExecuteCommand("x", null);
        }

        private void CCWStep_Click(object sender, EventArgs e)
        {
            Device.ExecuteCommand("z", null);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            double temp;

            temp = Convert.ToDouble(textBox1.Text);
            temp++;
            temp = temp - temp % (1.8);

            textBox1.Text = temp.ToString();

            temp = temp / (1.8);

            textBox2.Text = temp.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length != 0)
            {
                string temp = "m";

                if (radioButton1.Checked == true)
                {
                    temp = temp + "1";
                }
                else
                {
                    temp = temp + "0";
                }

                temp = temp + Convert.ToInt16(textBox2.Text).ToString("X4");

                //Исполняем команду (отправляем ее на устройство)
                Device.ExecuteCommand(temp, wait_succes);

                button1.Enabled = false;
                but_status = false;

                //Ожидаем завершения вращения
                while (but_status == false) ;

                but_status = true;
                button1.Enabled = true;
            }
        }

        private void wait_succes(string arg)
        {
            //Сюда вернется ответ от девайса

            if (arg == "done")
            {
                but_status = true;
            }
        }
    }
}
